<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util\Tree;
class ShopCateController extends Controller
{
    /**
     * 店铺商品分类展示
     */
     public function index ()
     {
         $where['shop_id'] = 5;
         $cates = M('shop_cate')->where($where)->select();

         $data=array();
         foreach($cates as $k=>$v){
             $data[$v['id']]=$v;
         }

         $da=array();
         foreach($data as $k=>$v){
             if(isset($data[$v['pid']])){
                 $data[$v['pid']]['son'][] =& $data[$v['id']];
             }else{
                 $da[] =& $data[$v['id']];
             }
         }

         $this->assign('list',$da);
         $this->display();

     }
   
   public function first($shop_id)
   {
        $shopcate = D('ShopCate');
        return $shopcate->first($shop_id, 0);
   }

   public function getCat($shop_id, $pid)
   {
        $shopcate = D('ShopCate');
        $info = $shopcate->first($shop_id, $pid);
        echo json_encode($info, JSON_UNESCAPED_UNICODE);
   }

    /**
     * 删除分类
     */
    public function del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : false;

        $data['pid'] = $id;
        $cates = M('shop_cate')->where($data)->select();
        if($cates){
           echo json_error('有下级分类，不能删除');
        }else{
            $where['id'] = $id;
            $result = M("shop_cate")->where($where)->delete();
            if($result){
               echo json_success('删除成功');
            }else{
              echo json_error('删除失败');
            }
        }

    }

    /**
     * from表单
     */
    public function form ()
    {
        $where['shop_id'] = 5;
        $id = $_GET['id'];

        $cates = M('shop_cate')->where($where)->select();

        $data=array();
        foreach($cates as $k=>$v){
            $data[$v['id']]=$v;
        }

        $tree= new Tree($data,'id','pid','cat_name');

        $tree_cate=$tree->get_tree(0, $id);

        $this->assign('tree',$tree_cate);
        $this->display();
    }

    /**
     * 添加分类
     */
    public function add()
    {
        $data['shop_id'] = 5;
        $data['cat_name'] = $_POST['cat_name'];
        $data['pid'] = $_POST['pid'];

        $cate = M('shop_cate');
        if($cate->add($data)){
            echo json_success('添加成功');
        }else{
            echo json_error('添加失败');
        }
    }

    /**
     * 编辑用的form表单
     */
    public function editForm ()
    {
        $where['shop_id'] = 5;
        $where['id'] = $_GET['nid'];
        $id = $_GET['id'];

        $cate = M('shop_cate')->where($where)->select();

        $cates = M('shop_cate')->where($where)->select();

        $data=array();
        foreach($cates as $k=>$v){
            $data[$v['id']]=$v;
        }

        $tree= new Tree($data,'id','pid','cat_name');
        $tree_cate=$tree->get_tree(0, $id);

        $this->assign('tree',$tree_cate);
        $this->assign('cate',$cate);

        $this->display();
    }

    /**
     * 编辑
     */
    public function edit ()
    {
        $where['shop_id'] = 5;
        $where['id'] = $_POST['id'];

        $data['cat_name'] = $_POST['cat_name'];
        $cate = M('shop_cate');

        if($cate->where($where)->save($data)){
            echo json_success('编辑成功');
        }else{
            echo json_error('编辑失败');
        }
    }

}